package com.laicunba.ext.web.controller.h5;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.ChannelDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.ChannelService;
import com.laicunba.service.RegistryService;
import com.laicunba.util.StringUtil;
import com.laicunba.util.UserAgentUtil;

@Controller
@RequestMapping(value = "/h5/ext")
public class DownloadFingerprintController extends BaseController {

	@Autowired
	private ChannelService channelService;
	@Autowired
	private RegistryService registryService;

	@ResponseBody
	@RequestMapping(value = "/download/{channel}.htm")
	public void download(@PathVariable("channel") String channel, HttpServletResponse response) {
		HttpServletRequest req = getRequest();
		System.out.println(req.getHeader("User-Agent"));
		String agent[] = UserAgentUtil.parse(req.getHeader("User-Agent"));
		String ip = req.getHeader("X-Real-IP");
		ip = StringUtil.isEmpty(ip) ? req.getRemoteAddr() : ip;
		ChannelDO channelDO = new ChannelDO();
		channelDO.setDlIp(ip);
		channelDO.setDlOs(agent[0]);
		channelDO.setChannel(channel);
		channelDO.setDlSn("iphone".equalsIgnoreCase(agent[0]) ? agent[1] : agent[2]+"-"+agent[1]);
		channelDO.setDlTime(new Date());
		channelService.create(channelDO);
		try {
			RegistryDO registryDO = registryService.get(RegistryDO.class, "sys.channel.download.url");
			response.sendRedirect(registryDO != null ? registryDO.getVal() : "http://a.app.qq.com/o/simple.jsp?pkgname=com.laicunba.licai&ckey=CK1360261798014");
		} catch (IOException e) {
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/download_ios/{channel}.htm")
	public void downloadIos(@PathVariable("channel") String channel, HttpServletResponse response) {
		HttpServletRequest req = getRequest();
		System.out.println(req.getHeader("User-Agent"));
		String agent[] = UserAgentUtil.parse(req.getHeader("User-Agent"));
		String ip = req.getHeader("X-Real-IP");
		ip = StringUtil.isEmpty(ip) ? req.getRemoteAddr() : ip;
		ChannelDO channelDO = new ChannelDO();
		channelDO.setDlIp(ip);
		channelDO.setDlOs(agent[0]);
		channelDO.setChannel(channel);
		channelDO.setDlSn("iphone".equalsIgnoreCase(agent[0]) ? agent[1] : agent[2]+"-"+agent[1]);
		channelDO.setDlTime(new Date());
		channelService.create(channelDO);
		try {
			RegistryDO registryDO = registryService.get(RegistryDO.class, "sys.channel.download_ios.url");
			response.sendRedirect(registryDO != null ? registryDO.getVal() : "https://itunes.apple.com/cn/app/lai-cun-ba-li-cai-gao-shou/id1087649876?mt=8");
		} catch (IOException e) {
		}
	}
}
